﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLKS.Templates;
using System.Data;

namespace QLKS.Views
{
    /// <summary>
    /// Interaction logic for frmGuiTinNhan.xaml
    /// </summary>
    public partial class frmGuiTinNhan : FormTrans
    {
        public bool UpdateSucces = false;
        DataTable tb = new DataTable();
       
        public frmGuiTinNhan()
        {
            InitializeComponent();
            EnterAsTab = false;
        }

        private bool KiemTraThongTin()
        {
            string[] str = txtNguoiNhan.Text.Split(',',';');
            foreach (string s in str)
                if (!Models.NguoiDungModel.CheckUser(s))
                {
                    MessageBox.Show(M_LAN.Equals("V") ? "Người dùng " + s + " không tồn tại!" : "User name " + s + " not Exists!", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                    int index = txtNguoiNhan.Text.IndexOf(s);
                    txtNguoiNhan.Select(index, s.Length);
                    txtNguoiNhan.Focus();
                    txtNguoiNhan.CaretIndex = index;
                    return false;
                }
            if (string.IsNullOrEmpty(txtNguoiNhan.Text))
            {
                MessageBox.Show(M_LAN.Equals("V") ? "Chưa vào tên nhười nhận!" : "Input to username, please!", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtNguoiNhan.Focus();
                return false;
            }
            if(string.IsNullOrEmpty(txtTieuDe.Text))
            {
                MessageBox.Show(M_LAN.Equals("V")?"Chưa nhập tiêu đề tin nhấn!":"Input header of message, please!","QLKS",MessageBoxButton.OK,MessageBoxImage.Error);
                txtTieuDe.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtNoiDung.Text))
            {
                MessageBox.Show(M_LAN.Equals("V") ? "Chưa nhập nội dung tin nhấn!" : "Input content of message, please!", "QLKS", MessageBoxButton.OK, MessageBoxImage.Error);
                txtNoiDung.Focus();
                return false;
            }
            return true;
        }

        private void GuitinNhan()
        {
            if (!KiemTraThongTin())
                return;
            if (Models.TinNhanModel.GuiTinNhan(tb) > 0)
            {
                MessageBox.Show(M_LAN.Equals("V")?"Gửi tin nhấn thành công!":"Send Message sucsess!","QLKS",MessageBoxButton.OK);
                UpdateSucces = true;
                this.Close();
            }
        }

        private void FormTrans_Loaded(object sender, RoutedEventArgs e)
        {
            tb = Models.TinNhanModel.CreateNewTinNhan(frmMain._UserName);
            gridMain.DataContext = tb;
            txtNguoiNhan.Focus();
        }

        private void btnReply_Click(object sender, RoutedEventArgs e)
        {
            GuitinNhan();
        }

        private void txtNoiDung_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            string noidung = txtNoiDung.Text;
            txtNoiDung.Text = "";
            txtNoiDung.AppendText("Tiêu đề: " + txtTieuDe.Text + "\n");
            txtNoiDung.AppendText("----------------------------------\n");
            txtNoiDung.AppendText("Người gửi: " + frmMain._UserName + "\n");
            txtNoiDung.AppendText("Thời gian: " + DateTime.Now.ToShortTimeString() + "\n");
            txtNoiDung.AppendText("----------------------------------\n");
            txtNoiDung.AppendText(noidung);
            tb.Rows[0]["noidung"] = txtNoiDung.Text;
        }
    }
}
